﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Collections;
using FeatureModelCore;
using System.Windows;
using FeatureModelTool.Windows;
using System.IO;
using System.Windows.Media;
using FeatureModelCore.Configuration;

namespace FeatureModelTool.FileView
{
    delegate TabItem GenerateFileTab(string fileFullPath);
    public class FileViewer : TabControl
    {
        #region Fields
        private Hashtable _fileViewerRegisterTable;
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        public FileViewer()
        {
            _fileViewerRegisterTable = new Hashtable();
            // register Feature Tree View
            GenerateFileTab generator = GenerateFeatureTreeView;
            _fileViewerRegisterTable.Add(FileLoaderType.FeatureTreeView
                , generator);
            generator = GenerateFeaturePlainText;
            _fileViewerRegisterTable.Add(FileLoaderType.Text
                , generator);
            generator = GenerateFeatureGraphic;
            _fileViewerRegisterTable.Add(FileLoaderType.FeatureGraphicalView
                , generator);
            generator = GenerateConfigTreeView;
            _fileViewerRegisterTable.Add(FileLoaderType.ConfigTreeView
                , generator);

            this.SizeChanged += new SizeChangedEventHandler(TabControl_SizeChanged);
        }
        #endregion

        private void TabControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            FileViewer tab = sender as FileViewer;
            if (tab == null)
            {
                return;
            }

            // for all tabs in the tab control
            foreach (TabItem tabItem in tab.Items)
            {
                if(tabItem is FeatureModelGraphicalTab)
                {
                    FeatureModelGraphicalTab gTab = (FeatureModelGraphicalTab)tabItem;
                    gTab.TabItem_SizeChanged();
                }
            }

        }

        /// <summary>
        /// Construct New Tab
        /// </summary>
        /// <param name="fileFullPath"></param>
        /// <param name="type"></param>
        public void ConstructNewTab(string fileFullPath, FileLoaderType type)
        {
            if (this.ContainFile(fileFullPath))
            {
                throw new Exception("The file already opened.");
            }

            GenerateFileTab generator = (GenerateFileTab)_fileViewerRegisterTable[type];
            TabItem newTab = generator(fileFullPath);
            //newTab.ContextMenu = GenerateContextMenu(this, newTab);
            this.Items.Add(newTab);
            newTab.Focus();
        }
        /// <summary>
        /// Save all files
        /// </summary>
        public void SaveAllFiles()
        {
            foreach (TabItem item in Items)
            {
                if (item is FeatureModelTreeViewTab)
                {
                    FeatureModelTreeViewTab treeViewTab = (FeatureModelTreeViewTab)item;
                    FeatureModelTree fm = (FeatureModelTree)treeViewTab.FeatureModelView.TreeInstance;
                    fm.Save();
                }
                else if (item is FeatureModelGraphicalTab)
                {
                    FeatureModelGraphicalTab graphicalTab = (FeatureModelGraphicalTab)item;
                    graphicalTab.GraphicalView.TreeInstance.Save();
                }
            }
        }
        /// <summary>
        /// Save current forcus file
        /// </summary>
        public void SaveCurrentFile()
        {
            TabItem focusTab = GetCurrentFocusTab();
            if (focusTab == null)
            {
                return;
            }
            if (focusTab is FeatureModelTreeViewTab)
            {
                FeatureModelTreeViewTab treeViewTab = (FeatureModelTreeViewTab)focusTab;
                FeatureModelTree fm = (FeatureModelTree)treeViewTab.FeatureModelView.TreeInstance;
                fm.Save();
            }
        }
        /// <summary>
        /// Is Dirty
        /// </summary>
        /// <returns></returns>
        public bool IsDirty()
        {
            foreach (TabItem tab in this.Items)
            {
                IMainTab mainTab = tab as IMainTab;
                if (mainTab != null && mainTab.Dirty)
                {
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// Get Current Focus Tab
        /// </summary>
        /// <returns></returns>
        public TabItem GetCurrentFocusTab()
        {
            foreach (TabItem item in this.Items)
            {
                if (item.IsFocused == true)
                {
                    return item;
                }
            }
            return null;
        }
        /// <summary>
        /// Contain full file
        /// </summary>
        /// <param name="fileFullPath"></param>
        /// <returns></returns>
        public bool ContainFile(string fileFullPath)
        {
            foreach (TabItem item in this.Items)
            {
                if (item is FeatureModelTreeViewTab)
                {
                    FeatureModelTreeViewTab treeViewTab = (FeatureModelTreeViewTab)item;
                    FeatureModelTree fm = (FeatureModelTree)treeViewTab.FeatureModelView.TreeInstance;
                    if (fm.FullFilePath == fileFullPath)
                    {
                        return true;
                    }
                }
                else if (item is FeatureModelGraphicalTab)
                {
                    FeatureModelGraphicalTab graphicalTab = (FeatureModelGraphicalTab)item;
                    FeatureModelTree fm = graphicalTab.GraphicalView.TreeInstance;

                    if (fm.FullFilePath == fileFullPath)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
        /// <summary>
        /// Generate
        /// </summary>
        /// <param name="fileFullPath"></param>
        /// <returns></returns>
        public static TabItem GenerateFeatureTreeView(string fileFullPath)
        {
            FeatureModelTree tree = FeatureModelTree.Load(fileFullPath);
            FeatureModelTreeViewTab tab = new FeatureModelTreeViewTab();

            tab.FeatureModelView.Bind(tree);
            tab.Header = tree.FileName;

            return tab;
        }
        /// <summary>
        /// Generate
        /// </summary>
        /// <param name="fileFullPath"></param>
        /// <returns></returns>
        public static TabItem GenerateFeaturePlainText(string fileFullPath)
        {
            string text = string.Empty;
            using (StreamReader reader = File.OpenText(fileFullPath))
            {
                text = reader.ReadToEnd();
            }
            TextViewTab tab = new TextViewTab(text, fileFullPath);

            return tab;
        }
        /// <summary>
        /// Generate Feature Tree Graphic
        /// </summary>
        /// <param name="fileFullPath"></param>
        /// <returns></returns>
        public static TabItem GenerateFeatureGraphic(string fileFullPath)
        {
            FeatureModelTree tree = FeatureModelTree.Load(fileFullPath);
            FeatureModelGraphicalTab tab = new FeatureModelGraphicalTab();

            tab.GraphicView.Bind(tree);
            tab.Header = tree.FileName;

            return tab;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileFullPath"></param>
        /// <returns></returns>
        public static TabItem GenerateConfigTreeView(string fileFullPath)
        {
            ConfigFeatureModel configFm = ConfigFeatureModel.Load(fileFullPath);
            ConfigTreeViewTab tab = new ConfigTreeViewTab();
            tab.ConfigTreeView.Bind(configFm);
            tab.Header = configFm.Root.Name;
            return tab;
        }
        ///// <summary>
        ///// Generate Tab context menu
        ///// </summary>
        ///// <param name="tab"></param>
        ///// <param name="item"></param>
        ///// <returns></returns>
        //public static ContextMenu GenerateContextMenu(TabControl tab, TabItem item)
        //{
        //    MenuItem_FileViewer close = new MenuItem_FileViewer(tab, item)
        //    {
        //        Name = "Close",
        //        Header = "Close"
        //    };
        //    close.Click += new RoutedEventHandler(close_EventHandler);

        //    ContextMenu menu = new ContextMenu();
        //    menu.Items.Add(close);
        //    return menu;
        //}
        /// <summary>
        /// close
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //public static void close_EventHandler(object sender, RoutedEventArgs e)
        //{
        //    MenuItem_FileViewer fViewer = sender as MenuItem_FileViewer;
        //    if (fViewer == null)
        //    {
        //        return;
        //    }

        //    if (IsDirty(fViewer.Item))
        //    {
        //        ClosingWarningDialog dialog = new ClosingWarningDialog();
        //        dialog.ShowDialog();
        //        if (dialog.ToSave == true)
        //        {
        //            if (fViewer.Item is FeatureModelTreeViewTab)
        //            {
        //                FeatureModelTreeViewTab treeView = (FeatureModelTreeViewTab)fViewer.Item;
        //                ((FeatureModelTree)treeView.FeatureModelView.TreeInstance).Save();
        //            }
        //        }
        //        if (dialog.ToClose == false)
        //        {
        //            return;
        //        }
        //    }

        //    fViewer.Tab.Items.Remove(fViewer.Item);
        //}
    }
}
